package com.wyyx.api;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wyyx.services.CouponTakeServices;

import commons.Constants;
import commons.JsonResult;
import commons.JsonResultWriter;

/**
 * 领取优惠券
 */
@WebServlet("/CouponTakeServlet")
public class CouponTakeServlet extends HttpServlet {
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String userId = request.getParameter("userId");
		String couponId = request.getParameter("couponId");
		CouponTakeServices couponTakeServices = new CouponTakeServices();
		int i = couponTakeServices.couponTakeService(userId, couponId);
		JsonResult result = null;
		try {
			if(i!=0) {
				result = new JsonResult("领取成功",Constants.STATUS_SUCCESS);
			}else {
				result = new JsonResult("领取失败",Constants.STATUS_NOTFOUND);
			}
		}catch(Exception e) {
			result = new JsonResult("领取异常",Constants.STATUS_EX);
		}
		JsonResultWriter.writer(response,result);
	}
	
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
